import Vue from "vue";
import VueRouter from "vue-router";
import {getToken, removeToken} from "@/utils/auth";
import {Dialog} from "vant";
Vue.use(VueRouter);
Vue.use(Dialog);

const routes = [
  {
    path: "/",
    redirect: "/main"
  },
  {
    path: "/main",
    component: () => import("views/TabbarMain/TabbarMain"),
    children: [
      {
        path: "/",
        redirect: "home"
      },
      {
        path: "home",
        name:'home',
        component: () => import("views/TabbarMain/Home/Home")
      },
      {
        path: "personal",
        component: () => import("views/TabbarMain/Personal/Personal")
      }
    ]
  },
  {
    path: "/news",
    component: () => import("views/News/News")
  },
  {
    path: "/login",
    component: () => import("views/Login/Login")
  },
  {
    path: "/registered",
    component: () => import("views/Login/registered")
  },
  {
    path: "/join",
    component: () => import("views/TabbarMain/Personal/Join")
  },
  {
    path: "/started",
    component: () => import("views/TabbarMain/Personal/Started")
  },
  {
    path: "/winning_record",
    component: () => import("views/TabbarMain/Personal/WinningRecord")
  },

  {
    path: "/setting",
    component: () => import("views/TabbarMain/Personal/Setting"),
  },
  {
    path: "/notice",
    component: () => import("views/TabbarMain/Personal/Notice")
  },
  {
    path: "/address",
    component: () => import("views/TabbarMain/Personal/Address")
  },
  {
    path: "/address_edit",
    component: () => import("views/TabbarMain/Personal/components/AddressEditPage")
  },
  {
    path: "/setting_modify",
    name: "setting_modify",
    component: () => import("views/TabbarMain/Personal/components/SettingChild")
  },
  {
    path:'/historical_records',
    component: () => import("views/TabbarMain/Personal/historicalRecords")
  },
  {
    path: "/retrieve",
    component: () => import("views/Login/Retrieve")
  },
  {
    path: "/up_password",
    component: () => import("views/Login/UpPassWord")
  },
  {
    path: "/contact",
    component: () => import("views/TabbarMain/Personal/Contact")
  }
]


const router = new VueRouter ({
  routes,
  mode: "history"
})

const whiteList = ['/news', "/main/home", "/main/personal", "/login", "/registered", "/retrieve", "/up_password"]

router.beforeEach((to, from, next) => {
  const hasToken = getToken();
  if (whiteList.indexOf(to.path) !== -1){
    next();
  }else{
    if(hasToken){
      next();
    }else{
      removeToken();
      Dialog.alert({
        title:"未登录",
        message: "请先登录"
      }).then(() => {
        next({path: "/login"})
      })
    }

  }
})

export default router
